package com.jic.manager.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.entity.JobManagement;
import com.jic.manager.mapper.JobManagementMapper;
import com.jic.manager.request.JobManagementRequest;
import com.jic.manager.response.JobManagementResponse;
import com.jic.manager.service.JobManagementService;
import com.jic.util.BaseResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 岗位管理
 * @author : 
 * @email : 
 * @since : 2020-02-26 09:40:52
 * @version : v1.0.0
 */
@Service
public class JobManagementServiceImpl implements JobManagementService{

    @Resource
    private JobManagementMapper jobManagementMapper;

    @Override
    public long insert(JobManagementRequest jobManagement) {
        JobManagement management = new JobManagement();
        BeanUtils.copyProperties(jobManagement,management);
        management.setCreator(jobManagement.getAdminOperate()!=null?jobManagement.getAdminOperate().getOperator():0);
        management.setCreatorName(jobManagement.getAdminOperate()!=null?jobManagement.getAdminOperate().getOperateName():"");
        long flag = jobManagementMapper.insert(management);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return jobManagementMapper.delete(id);
    }

    @Override
    public long update(JobManagementRequest jobManagement) {
        JobManagement management = new JobManagement();
        BeanUtils.copyProperties(jobManagement,management);
        management.setUpdater(jobManagement.getAdminOperate()!=null?jobManagement.getAdminOperate().getOperator():0);
        management.setUpdaterName(jobManagement.getAdminOperate()!=null?jobManagement.getAdminOperate().getOperateName():"");
        return jobManagementMapper.update(management);
    }

    @Override
    public JobManagement queryByPrimaryKey(Long id){
        return jobManagementMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<JobManagement> query(JobManagement jobManagement){
        return jobManagementMapper.query(jobManagement);
    }

    @Override
    public PageResult<JobManagement> queryPage(JobManagement jobManagement, Page pageQuery) {
        //计算下标 TODO 可以把计算下表抽取到公共pom里，作为工具类函数
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<JobManagement> list = jobManagementMapper.queryPage(startIndex, pageQuery.getLimit(), jobManagement);
        long count = jobManagementMapper.queryPageCount(jobManagement);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    /**
     * 岗位查询列表
     * @return
     */
    @Override
    public PageResult<JobManagementResponse> getJobList(JobManagementRequest jobManagementRequest) {
        //计算下标
        int startIndex = (jobManagementRequest.getStart() - 1) * jobManagementRequest.getLimit();
        jobManagementRequest.setStart(startIndex);

        List<JobManagementResponse> jobList = jobManagementMapper.getJobList(jobManagementRequest);
        Long jobListCount = jobManagementMapper.getJobListCount(jobManagementRequest);
        PageResult <JobManagementResponse>pageResult = new PageResult();
        pageResult.setRows(jobList);
        pageResult.setTotal(jobListCount);
        return pageResult;
    }

    @Override
    public RestResult<String> editJobInfo(JobManagementRequest jobManagementRequest) {
        try{
            JobManagement management = new JobManagement();
            BeanUtils.copyProperties(jobManagementRequest,management);
            Long id = jobManagementRequest.getId();
            // TODO 正向流程别取反  null 放判断符前面
            if(id !=/*这个有个空格*/null && id!=0){
                management.setUpdater(jobManagementRequest.getAdminOperate()!=null?jobManagementRequest.getAdminOperate().getOperator():0);
                management.setUpdaterName(jobManagementRequest.getAdminOperate()!=null?jobManagementRequest.getAdminOperate().getOperateName():"");
                jobManagementMapper.update(management);
            }else {
                management.setCreator(jobManagementRequest.getAdminOperate()!=null?jobManagementRequest.getAdminOperate().getOperator():0);
                management.setCreatorName(jobManagementRequest.getAdminOperate()!=null?jobManagementRequest.getAdminOperate().getOperateName():"");
                jobManagementRequest.setDeleteFlag(0);
                jobManagementMapper.insert(management);
            }
            return RestResult.success("成功");
        }catch (Exception e){
            // TODO
           e.printStackTrace();
           return RestResult.error("9999",e.getLocalizedMessage());
        }
    }

    @Override
    public RestResult<String> deleteJobInfo(JobManagementRequest jobManagementRequest) {
        try{
            JobManagement management = new JobManagement();
            BeanUtils.copyProperties(jobManagementRequest,management);
            management.setDeleteFlag(1);
            management.setUpdater(jobManagementRequest.getAdminOperate()!=null?jobManagementRequest.getAdminOperate().getOperator():0);
            management.setUpdaterName(jobManagementRequest.getAdminOperate()!=null?jobManagementRequest.getAdminOperate().getOperateName():"");

            if(StringUtils.isEmpty(jobManagementRequest.getIds())) throw new Exception("必传参数ids为空!");
            String[] idList = jobManagementRequest.getIds().split(",");
            for (String id : idList) {
                management.setId(Long.valueOf(id));
            }
            return RestResult.success("成功");
        }catch (Exception e){
            // TODO
            e.printStackTrace();
            return RestResult.error("9999",e.getLocalizedMessage());
        }
    }
}